import numpy as np

print(np.__version__)

## create
arr = np.array([1,2,3,4,5])
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.arange(1,10)
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.array((1,2,3,4,5)) #tuple
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.array([[1,2,3,4,5],[1,2,3,4,5]])
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.array([[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]])
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.array((1,2,3,4,5),ndmin=5) #tuple
print(arr)
print(arr.ndim,arr.size,type(arr))

arr = np.array([1,2,3,4,5],ndmin=5) #tuple
print(arr)
print(arr.ndim,arr.size,type(arr))

## indexing
arr = np.array([1,2,3,4,5])
print(arr.size,arr[0],arr[-1])

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print(arr.size)
print('0 row last element: ', arr[0, -1])
print('last row first element: ', arr[-1, 0])

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr.size,arr.ndim)
print('0 row : ', arr[0]) # 一维，实际上arr = int[2,2,3]
print('0 row last element: ', arr[0, -1]) # 二维
print('last row first element: ', arr[-1, 0]) #二维访问
print('arr[0, -1, -2]',arr[0, -1, -2]) # x=0,y=-1 z= -2,得到 [4, 5, 6] 中的第二个项

## Slicing，使用的是range的语法
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print('slice:',arr)
print('slice=[1:5]',arr[1:5])
print('slice=[4:]',arr[4:])
print('slice=[:4]',arr[:4])
print('slice=[:-1]',arr[:-1])
print('slice=[0: :2]',arr[: :2])

### 2D array
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 1:4]) #[7,8,9]
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
print(arr[0:2, 1:4])#这个感觉就像列选择然后进行区域选择， 比较重点的部分

arr = np.array([10, 15, 20, 25, 30, 35, 40])
print(arr[1:4]) #from 2nd to 5th

## Iterating
arr = np.array([1,2,3,4,5])
def print1D( _arr:np.array,sep=' ',end=None):
    print('[',end='')
    size = _arr.shape[0]
    index = 0
    for x in _arr:
        index += 1
        if index < size:
            print(x, end=sep)
        else:
            print(x,end=']')
    print('',end=end)
print(arr)
print1D(arr)

arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr)
def print2D(_arr:np.array,sep =' ', end=None):
    print('[', end='')
    for row in _arr:
       print1D(row, sep=sep, end='')
    print('', end=end)
print2D(arr)

#Iterating Arrays Using ndither
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr):
    print(x,end=',') #格式不好看 1,2,3,4,5,6,7,8,
print()

#Iterating Arrays Using ndither with parameters
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
  print(x,end=',')
print()

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for x in np.nditer(arr[:, ::2]):#::2 表示从开始到结束，step=2
  print(x,end=',')
print()

## enumerate 返回index,value
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for idx, x in np.ndenumerate(arr):#idx 表示tuple类型的
    print(idx,':', x, end=';')
print()

# 将NArray视作集合set进行的操作
print('np.unique(np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])):',np.unique(np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])))
## union 等于s1 U s2
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.union1d(arr1, arr2)
print('[1, 2, 3, 4] || [3, 4, 5, 6]:', newarr)
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.intersect1d(arr1, arr2, assume_unique=True)
print('[1, 2, 3, 4] && [3, 4, 5, 6]:', newarr)
newarr = np.setdiff1d(arr1, arr2, assume_unique=True)
print('[1, 2, 3, 4] - [3, 4, 5, 6]:', newarr)
newarr = np.setdiff1d(arr2, arr1, assume_unique=True)
print('[3, 4, 5, 6] - [1, 2, 3, 4]:', newarr) #注意这两个跟减法一样，结果不同
set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])
newarr = np.setxor1d(set1, set2, assume_unique=True)
print('[1, 2, 3, 4] xor [3, 4, 5, 6]:', newarr) # 这个 A || B - （ A && B ）
